<?php
/**
 * 功能: 轮播图管理
 * User: 文丹
 * Date: 2018/7/9 0009
 * Time: 9:28
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;

class Banner extends Power{
    protected $beforeActionList = [
        'first'=> ['only'=>'bannerList,banner_Hold,banner_edit,down,up,banner_del'],

    ];
    protected function first(){
        $result = $this->testing();
    }

    /**
     *  轮播图 列表
     */
    public function bannerList(){

        $banners = Db::name('banner')->paginate(10);
        $count   = Db::name('banner')->count();

        $this->assign([
            'banners'=>$banners,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="8">暂无数据</td></tr>'
        ]);
        return  $this->fetch('banner:banner_list');

    }

    /**
     *  轮播图 添加
     */
    public function banner_add(){
        $banner_id = intval(input('id'));
        if(!empty($banner_id)){
            $banner = Db::name('banner')->where('id',$banner_id)->find();
          //  print_r($banner);
            if(empty($banner)){
                return json(['msg'=>'数据为空！','ok'=>false]);
            }
            $this->assign('banner',$banner);
        }

        return $this->fetch('banner:banner_add');
    }
    /**
     *  轮播 添加
     */
    public function banner_Hold(){
        $title    = trim(input('title'));
        $intro    = trim(input('intro'));
        $link     = trim(input('link'));
        $image    = trim(input('image'));
        $type     = intval(input('type'));

        if(empty($link)||empty($image)){
            return json(['msg'=>'图片或者跳转链接不能为空！','ok'=>false]);
        }
        $data = [
            'title'=>$title,
            'intro'=>$intro,
            'link'=>$link,
            'type'=>$type,
            'image'=>$image
        ];
        $result =  $result =  Db::name('banner')->insert($data);
        if($result){
            return json(['msg'=>'添加成功！','ok'=>true]);
        }else{
            return json(['msg'=>'添加失败！','ok'=>false]);
        }
    }

    /**
     *  轮播图 修改
     */
    function banner_edit(){
        $banner_id = intval(input('id'));
        $title     = trim(input('title'));
        $intro     = trim(input('intro'));
        $link      = trim(input('link'));
        $image     = trim(input('image'));
        $type     = intval(input('type'));

        if(empty($title)||empty($image)){
            return json(['msg'=>'请填写完整的数据！','ok'=>false]);
        }
        $data = [
            'title'=>$title,
            'intro'=>$intro,
            'link'=>$link,
            'type'=>$type,
            'image'=>$image
        ];
        $result = Db::name('banner')->where('id',$banner_id)->update($data);
        if(!empty($result)){
            return json(['msg'=>'修改成功！','ok'=>true]);
        }else{
            return json(['msg'=>'修改失败！','ok'=>false]);
        }
    }

    /**
     *  轮播图 下架
     */
    public function down(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数！','ok'=>false]);
        }
       $result = Db::name('banner')->where('id',$id)->update(['is_down'=>2]);
        if(!empty($result)){
            return json(['msg'=>'已下架！','ok'=>true]);
        }else{
            return json(['msg'=>'下架失败！','ok'=>false]);
        }

    }
    /**
     * 轮播图 发布
     */
    public function up(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数！','ok'=>false]);
        }
        $result = Db::name('banner')->where('id',$id)->update(['is_down'=>1]);
        if(!empty($result)){
            return json(['msg'=>'已发布！','ok'=>true]);
        }else{
            return json(['msg'=>'发布失败！','ok'=>false]);
        }
    }

    /**
     * 轮播图删除
     */
    public function banner_del(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数！','ok'=>false]);
        }
        $result = Db::name('banner')->where('id',$id)->delete();
        if(!empty($result)){
            return json(['msg'=>'已删除！','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败！','ok'=>false]);
        }
    }


}